package cn.sjx.auth.controller;

import cn.sjx.auth.annotation.Permission;
import cn.sjx.auth.domain.Permission1;
import cn.sjx.auth.domain.Role;
import cn.sjx.auth.query.PermissionQuery;
import cn.sjx.auth.query.RoleQuery;
import cn.sjx.auth.service.RoleService;
import cn.sjx.basic.utils.AjaxResult;
import cn.sjx.basic.utils.PageList;
import cn.sjx.dto.RolePermissionDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
@Permission(name="角色权限")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    RoleService roleService;

    @Permission(name = "通过ID查询" ,descs = "通过ID查询权限")
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Serializable id) {
        try {
            Role role = roleService.getById(id);
            return AjaxResult.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    @Permission(name = "查询所有" ,descs = "查询所有权限")
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public AjaxResult getAll() {

        try {
            List<Role> list = roleService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    @Permission(name = "新增/修改" ,descs = "新增/修改权限")
    @PutMapping
    @ApiOperation(value = "新增或更新" )
    public AjaxResult insertOrupdate(@RequestBody Role role) {
        //通过Objects.isNull来判断
        try {
            if (Objects.isNull(role.getId())) {
                roleService.insert(role);
                return AjaxResult.me();
            } else {
                roleService.update(role);
                return AjaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @Permission(name = "删除" ,descs = "删除部门")
    //按ID删除
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Serializable id){
        try {
            roleService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    @Permission(name = "批量删除" ,descs = "批量删除")
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            roleService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @Permission(name = "角色的分页查询",descs = "分页查询")
    @PostMapping
    public AjaxResult pageList(@RequestBody RoleQuery roleQuery) {
        PageList<Role> pageList =  roleService.pageList(roleQuery);
        return AjaxResult.me().setData(pageList);
    }

    @Permission(name = "设置角色的权限")
    //如果前端传过来一个对象，而后端没有接收的domain，则可以用map来接收,也可以自己定义一个DTO
    @PostMapping("/permission")
    public AjaxResult setRolePermission(@RequestBody RolePermissionDTO dto) {
        roleService.setRolePermission(dto);
        return AjaxResult.me();
    }


    @Permission(name = "通过角色id查询权限")
    @GetMapping("/permission/{roleId}")
    public AjaxResult getPermissionByRoleId(@PathVariable("roleId") Long roleId) {
        List<String> permissionSns =  roleService.getPermissionByRoleId(roleId);
        return AjaxResult.me().setData(permissionSns);
    }
}
